﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameControllerScript : MonoBehaviour {

	public bool ballShot = false;

	public int leftScore = 0;
	public int rightScore = 0;

	public int winScore = 3;

	public GameObject leftScoreDisplay;
	public GameObject rightScoreDisplay;
	public GameObject notificationDisplay;
	public GameObject ball;

	private BallScript ballObject;
	private bool winner = false;
	private Text notificationDisplayText;

	// Use this for initialization
	void Start () {
		ballObject = ball.GetComponent<BallScript> ();
		notificationDisplayText = notificationDisplay.GetComponent<Text> ();
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.R)){
			ballObject.reset();
			if (rightScore < leftScore){
				ballObject.startBall(-1.0f);
			}
			else {
				ballObject.startBall(1.0f);
			}
		}
		if (Input.GetKeyDown(KeyCode.E) && !ballObject.moving){
			notificationDisplay.SetActive (false);

			if (rightScore < leftScore){
				ballObject.startBall(-1.0f);
			}
			else {
				ballObject.startBall(1.0f);
			}

			if (winner == true){
				ballObject.deathParticles.Stop();
				leftScore = 0;
				rightScore = 0;
				notificationDisplay.SetActive(false);
				winner = false;
			}
		}

		leftScoreDisplay.GetComponent<Text>().text = leftScore.ToString();
		rightScoreDisplay.GetComponent<Text>().text = rightScore.ToString();

		if (leftScore == winScore || rightScore == winScore) {
			ballObject.trailParticles.emissionRate = 0;
			if (leftScore > rightScore){
				notificationDisplayText.text = "Left player wins!";
			}
			else {
				notificationDisplayText.text = "Right player wins!";
			}
			notificationDisplay.SetActive (true);
			winner = true;
		}
	}
}

